<?php
	session_start();
	
	// sent from the timesheet form
	// 
	$EmployeeID=$_SESSION["employee_id"];
	$Date=$_POST['Date'];
	$JobDescript=$_POST['JobDescript'];
	$JobType=$_POST['JobType'];
	$FuelP=$_POST['FuelPurch'];
	$BoolDay=$_POST['Real'];
	$TTid=$_POST['TandT'];
	$BeginMile=$_POST['BeginMile'];
	$EndMile=$_POST['EndMile'];
	$TotMile=$_POST['TotMile'];
	$CCType=$_POST['CCType'];
	$City=$_POST['City'];
	$State=$_POST['State'];
	$Odometer=$_POST['Odometer'];
	$TotGallons=$_POST['TotGallon'];
	$TotCost=$_POST['TotCost'];
	$JobDescription=$_POST['JobDescription'];
	$CarDescription=$_POST['CarDescription'];
	$InTimeH=$_POST['InTimeH'];
	$InTimeM=$_POST['InTimeM'];
	$InTimeA=$_POST['InTimeAPM'];
	$OutTimeH=$_POST['OutTimeH'];
	$OutTimeM=$_POST['OutTimeM'];
	$OutTimeA=$_POST['OutTimeAPM'];
	$InLunchH=$_POST['InLunchH'];
	$InLunchM=$_POST['InLunchM'];
	$InLunchA=$_POST['InLunchAPM'];
	$OutLunchH=$_POST['OutLunchH'];
	$OutLunchM=$_POST['OutLunchM'];
	$OutLunchA=$_POST['OutLunchAPM'];
	$CityStart=$_POST['CityS'];
	$StateStart=$_POST['StateS'];
	$CityEnd=$_POST['CityE'];
	$StateEnd=$_POST['StateE'];
	
	
	$InTime=$InTimeH.':'.$InTimeM.' '.$InTimeA;
	$OutLunch=$OutLunchH.':'.$OutLunchM.' '.$OutLunchA;
	$InLunch=$InLunchH.':'.$InLunchM.' '.$OutLunchA;
	$OutTime=$OutTimeH.':'.$OutTimeM.' '.$OutLunchA;

	// protect from sql injections
	$EmployeeID = stripslashes($EmployeeID);
	$Date = stripslashes($Date);
	$JobDescript=stripslashes($JobDescript);
	$JobType=stripslashes($JobType);
	$BoolDay = stripslashes($BoolDay);
	$InTime = stripslashes($InTime);
	$OutLunch = stripslashes($OutLunch);
	$InLunch = stripslashes($InLunch);
	$OutTime = stripslashes($OutTime);
	$TTid = stripslashes($TTid);
	$BeginMile = stripslashes($BeginMile);
	$EndMile = stripslashes($EndMile);
	$TotMile = stripslashes($TotMile);
	$CCType = stripslashes($CCType);
	$City = stripslashes($City);
	$State = stripslashes($State);
	$Odometer = stripslashes($Odometer);
	$TotGallons = stripslashes($TotGallons);
	$TotCost = stripslashes($TotCost);
	$JobDescription = stripslashes($JobDescription);
	$CityStart=stripslashes($CityStart);
	$StateStart=stripslashes($StateStart);
	$CityEnd=stripslashes($CityEnd);
	$StateEnd=stripslashes($StateEnd);
	

	

	$EmployeeID = mysql_real_escape_string($EmployeeID);
	$Date = mysql_real_escape_string($Date);
	$JobDescript=mysql_real_escape_string($JobDescript);
	$JobType=mysql_real_escape_string($JobType);
	$BoolDay = mysql_real_escape_string($BoolDay);
	$InTime = mysql_real_escape_string($InTime);
	$OutLunch = mysql_real_escape_string($OutLunch);
	$InLunch = mysql_real_escape_string($InLunch);
	$OutTime = stripslashes($OutTime);
	$TTid = mysql_real_escape_string($TTid);
	$BeginMile = mysql_real_escape_string($BeginMile);
	$EndMile = mysql_real_escape_string($EndMile);
	$TotMile = mysql_real_escape_string($TotMile);
	$CCType = mysql_real_escape_string($CCType);
	$City = mysql_real_escape_string($City);
	$State = mysql_real_escape_string($State);
	$Odometer = mysql_real_escape_string($Odometer);
	$TotGallons = mysql_real_escape_string($TotGallons);
	$TotCost = mysql_real_escape_string($TotCost);
	$JobDescription = mysql_real_escape_string($JobDescription);
	$CityStart=mysql_real_escape_string($CityStart);
	$StateStart=mysql_real_escape_string($StateStart);
	$CityEnd=mysql_real_escape_string($CityEnd);
	$StateEnd=mysql_real_escape_string($StateEnd);
		

	//Seperating some strings
	$JobDescript=str_replace('Job Name: ', '', $JobDescript);
	$JobDescript=str_replace('Job Number: ', '', $JobDescript);
	$JobNumber=substr($JobDescript, 0, stripos($JobDescript," "));
	$JobName=str_replace($JobNumber, '', $JobDescript);
	
	$CCName=substr($CCType, 0, stripos($CCType," "));
	$CCNumber=str_replace($CCName, '', $CCType);
	$CCNumber=str_replace(' ', '',$CCNumber);
		
	//make connection to database, bail if no connection
	$connection = odbc_pconnect('utrocks','','');
	if (!$connection) { exit("Connection Failed: " . $connection); }
	
	//Insert Fields into DB
	
	$sql = "INSERT INTO TimeSheet (EmployeeID, EntryDate, JobNumber, JobName, BoolDay, InTime, OutLunch, InLunch, OutTime, JobType, Description) VALUES('$EmployeeID','$Date','$JobNumber','$JobName','$BoolDay','$InTime','$OutLunch','$InLunch','$OutTime','$JobType','$JobDescription')";
	$rs = odbc_exec($connection, $sql);
	if (!$rs) { exit("Error in SQL"); }


if(($JobType == 'Pickup') OR ($JobType == 'Return') OR ($JobType == 'Transport') OR ($JobType == 'Travel')){
	
	foreach($CarDescription as $Car){
		$parts=explode(" ", $Car);
		$VIN=$parts[0];
		$Make=$parts[1];
		$Model=$parts[2];
		$CarYear=$parts[3];
		$ExColor=$parts[4];
		$InColor=$parts[5];
		
		$sql = "INSERT INTO Travel (EmployeeID, EntryDate, JobNumber, JobName, MileStart, MileEnd, TotMiles, TruckID, CityStart, StateStart, CityEnd, StateEnd, VIN, CarYear, Make, Model, ExColor, InColor, JobType) VALUES('$EmployeeID', '$Date', '$JobNumber', '$JobName', '$BeginMile', '$EndMile', '$TotMile', '$TTid', '$CityStart', '$StateStart', '$CityEnd', '$StateEnd', '$VIN', '$CarYear', '$Make', '$Model', '$ExColor', '$InColor', '$JobType')";
		$rs = odbc_exec($connection, $sql);
		if (!$rs) { exit("Error in SQL"); }
	}
}
	
if($FuelP == 'Fuel Purchased'){	
	$sql = "INSERT INTO Purchase (EmployeeID, EntryDate, JobNumber, JobName, CCNumber, CCType, City, State, TotalCost, Odometer, TotalGallons) VALUES('$EmployeeID', '$Date', '$JobNumber', '$JobName', '$CCNumber', '$CCName', '$City', '$State', '$TotCost', '$Odometer', '$TotGallons')";
	$rs = odbc_exec($connection, $sql);
	if (!$rs) { exit("Error in SQL"); }
}
	//close connection
	odbc_close($connection);
?>

<script type="text/javascript"> 

</script>